<?php

namespace app\api\controller\shop;


use app\api\model\shop\Goods as GoodsModel;
use app\api\model\shop\SkuSpec;
use app\api\model\shop\Category;
use app\common\controller\Api;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //详情
    public function detail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误');
        }
        $row = (new GoodsModel())->with([
            'Sku',

        ])->where('status', '<>', 'hidden')->where('id', $id)->find();

        if (!$row) {
            $this->error('未找到该商品');
        }
        $row->setInc('views');

        $row->sku_spec = SkuSpec::getGoodsSkuSpec($id);

        $row->visible(explode(',', 'id,title,subtitle,category_id,price,marketprice,sales,views,image,content,images,sku_spec,sku,comment,is_collect,guarantee,attributes,favor_rate,coupon,stocks'));
        $row = $row->toArray();

        $share_good_id = \app\common\model\Config::get(['name'=>'share_good_id'])->value;
        $pv_ratio = \app\common\model\Config::get(['name'=>'pv_ratio'])->value;
        if ($share_good_id==$id){
            $pv = 0;
        } else {
            $pv = bcmul($row['price'],$pv_ratio,1);
        }
        $row['pv'] = $pv;
        $this->success('获取成功', $row);
    }

    //列表
    public function lists()
    {
        $param = $this->request->param();
        $pageNum = (int)$this->request->param('pageNum', 10);
        $orderby = $this->request->param('orderby', 'weigh');
        $orderway = $this->request->param('orderway', 'desc');

        $list = GoodsModel::where(function ($query) use ($param) {
            $query->where('status', 'normal');
            //关键词
            if (isset($param['keyword']) && !empty($param['keyword'])) {
                $query->where('title|keywords', 'like', '%' . $param['keyword'] . '%');

            }
            //分类
            if (isset($param['category_id']) && !empty($param['category_id'])) {
                $query->where('category_id', 'IN',Category::getCategoryChildrenIds($param['category_id']));
            }


            //价格
            if (isset($param['price']) && !empty($param['price'])) {
                $priceArr = explode('-', $param['price']);
                if (count($priceArr) == 2) {
                    if (isset($priceArr[0])) {
                        $priceArr[0] = (float)$priceArr[0];
                    }
                    if (isset($priceArr[1])) {
                        $priceArr[1] = (float)$priceArr[1];
                    }
                    $query->where('price', 'BETWEEN', $priceArr);
                }
            }
        })->order("{$orderby} {$orderway}")->paginate($pageNum);

        $share_good_id = \app\common\model\Config::get(['name'=>'share_good_id'])->value;
        $pv_ratio = \app\common\model\Config::get(['name'=>'pv_ratio'])->value;

        foreach ($list as $k=>$v){
            if ($share_good_id==$v['id']){
                $list[$k]['pv'] = 0;
            } else {
                $list[$k]['pv'] = bcmul($v['price'],$pv_ratio,1);
            }
        }

        foreach ($list as $item) {
            $item->visible(explode(',', 'id,title,image,price,sales,views,description,marketprice,createtime,pv'));
        }


        $this->success('', $list);
    }


}